// pages/repertory/repertory.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabList: ["单选题", "多选题", "判断题"],
    selectIndex: 0,
    questionList: [],
    type: 1,
    category: 0,
    showPage: false,
    page: 0,
    showLoading: false,
    loadingText: "加载中...",
    page_count: ""
  },
  changeTab(e){
    // console.log(e)
    let index = e.currentTarget.dataset.index
    this.setData({
      selectIndex: index,
      type: index+1,
      page: 0,
      questionList: [],
    })
    wx.showLoading()
    this.getQuetionList()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading()
    wx.setNavigationBarTitle({
      title: options.cateName
    })
    let category = options.category
    this.setData({
      category: category
    })
    this.getQuetionList()
  },
  getQuetionList(){
    let obj={
      page: this.data.page,
      size: 8
    }
    app.api.getQuetionList(this.data.type, this.data.category, obj)
    .then(res => {
      wx.hideLoading()
      let questionList = res.content
      questionList.forEach(item => {
        item.title = JSON.parse(item.body).title
        let arr = []
        let answer = item.answer
        item.options.forEach((item,index) => {
          arr.push({
            title: this.formatOpt(index+1)+item,
            checked: answer.indexOf(String(index)) != -1
          })
        })
        item.options = arr
      })
      this.setData({
        questionList: this.data.questionList.concat(questionList),
        showPage: true,
        showLoading: false,
        page_count: res.totalPages
      })
    })
  },
  formatOpt(index) {
    switch (index) {
      case 1:
        return "A "
        break;
      case 2:
        return "B "
        break;
      case 3:
        return "C "
        break;
      case 4:
        return "D "
        break;
      case 5:
        return "E "
        break;
      case 6:
        return "F "
        break;
      case 7:
        return "G "
        break;
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(111)
    if (this.data.page + 1 == this.data.page_count) {
      this.setData({
        showLoading: true,
        loadingText: "已经到底了！"
      })
    } else {
      this.setData({
        page: this.data.page + 1,
        showLoading: true,
        loadingText: "加载中..."
      })
      this.getQuetionList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})